package com.cyz.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.admin.service.CardService;
import com.cyz.common.bean.Card;
import com.cyz.common.bean.CardButton;
import com.cyz.common.dto.CardDto;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.CardButtonMapper;
import com.cyz.common.mapper.CardMapper;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import com.cyz.admin.vo.AdminCardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 34928
 * @description 针对表【card】的数据库操作Service实现
 * @createDate 2023-02-21 08:14:10
 */
@Service
@Transactional
public class CardServiceImpl extends ServiceImpl<CardMapper, Card>
        implements CardService{

    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private CardButtonMapper cardButtonMapper;

    @Override
    public R queryCards() {
//        //创建筛选器
//        LambdaQueryWrapper<Card> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        //根据更新时间排序
//        lambdaQueryWrapper.orderByDesc(Card::getUpdateTime);
//        //查询相关课程
//        List<Card> list = list(lambdaQueryWrapper);

        //根据更新时间排序
        //查询相关课程
        List<Card> list = cardMapper.queryCards();

        //vo处理
        List<AdminCardVo> adminCardVos = BeanCopyUtil.copyBeanList(list, AdminCardVo.class);
        //返回数据
        return R.success(adminCardVos);
    }

    @Override
    public R queryCardsAndButtons() {
        //创建筛选器
//        LambdaQueryWrapper<Card> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        //根据置顶排序
//        lambdaQueryWrapper.orderByDesc(Card::getIsTop);
        //根据更新时间排序
//        lambdaQueryWrapper.orderByDesc(Card::getUpdateTime);
        //查询相关课程
        List<Card> list = cardMapper.queryCards();
        //vo处理
        List<AdminCardVo> adminCardVos = BeanCopyUtil.copyBeanList(list, AdminCardVo.class);
        adminCardVos.forEach(item->{
            item.setButtonNames(cardButtonMapper.getbuttonNamesByCardId(item.getId()));
            item.setButtonIds(cardButtonMapper.getbuttonIdsByCardId(item.getId()));
        });
        //返回数据
        return R.success(adminCardVos);
    }

    @Override
    public R updateCard(CardDto cardDto) {
        //dto转化
        Card card = BeanCopyUtil.copyBean(cardDto, Card.class);
//        //更新数据
//        boolean flag = updateById(card);
        //更新数据
        boolean flag = cardMapper.updateCard(card);
//        //删除课程所有的按钮关系
//        cardButtonService.removeById(cardDto.getId());

        //删除课程所有的按钮关系
        cardButtonMapper.removeById(cardDto.getId());
        //重新添加
        cardDto.getButtonIds().forEach(item ->{
            cardButtonMapper.save(new CardButton(cardDto.getId(),item));
        });
        //错误判断
        if(flag){
            return queryCardsAndButtons();
        }
        return R.error(MyEnums.UPDATE_FAIL);
    }

    @Override
    public R addCard(CardDto cardDto) {
        //dto转化
        Card card = BeanCopyUtil.copyBean(cardDto, Card.class);
        //更新数据
        boolean flag = cardMapper.addCard(card);
        //获取新增数据id
        Integer id = card.getId();
        //取出课程分类
        List<Integer> list = cardDto.getButtonIds();
        list.forEach(item ->{
            cardButtonMapper.save(new CardButton(id,item));
        });

        //错误判断
        if(flag){
            return queryCardsAndButtons();
        }
        return R.error(MyEnums.ADD_FAIL);
    }

    @Override
    public R deleteCards(Integer[] ids) {
//        //删除数据
//        boolean flag = removeByIds(Arrays.asList(ids));

        //删除数据
        boolean flag = cardMapper.removeByIds(ids);

        //错误判断
        if(flag){
            return queryCardsAndButtons();
        }
        return R.error(MyEnums.DELETE_FAIL);
    }
}